﻿using Dapper;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace B.S.InOut.Infacation
{
    // 通用仓储接口的实现类，使用 Dapper 进行数据库操作
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        private readonly string _connectionString;

        public BaseRepository(string connectionString)
        {
            _connectionString = connectionString;
        }

        // 插入实体
        public async Task<int> InsertAsync(T entity)
        {
            using (IDbConnection db = new SqlConnection(_connectionString))
            {
                var sql = $"INSERT INTO {typeof(T).Name} ({string.Join(", ", typeof(T).GetProperties().Select(p => p.Name))}) VALUES ({string.Join(", ", typeof(T).GetProperties().Select(p => $"@{p.Name}"))})";
                return await db.ExecuteAsync(sql, entity);
            }
        }

        // 根据 ID 获取实体
        public async Task<T> GetByIdAsync(int id)
        {
            using (IDbConnection db = new SqlConnection(_connectionString))
            {
                var sql = $"SELECT * FROM {typeof(T).Name} WHERE {typeof(T).GetProperties().First(p => p.GetCustomAttributes(typeof(KeyAttribute), true).Length > 0).Name} = @Id";
                return await db.QueryFirstOrDefaultAsync<T>(sql, new { Id = id });
            }
        }

        // 获取所有实体
        public async Task<IEnumerable<T>> GetAllAsync()
        {
            using (IDbConnection db = new SqlConnection(_connectionString))
            {
                var sql = $"SELECT * FROM {typeof(T).Name}";
                return await db.QueryAsync<T>(sql);
            }
        }

        // 更新实体
        public async Task<int> UpdateAsync(T entity)
        {
            using (IDbConnection db = new SqlConnection(_connectionString))
            {
                var properties = typeof(T).GetProperties().Where(p => p.Name != typeof(T).GetProperties().First(p => p.GetCustomAttributes(typeof(KeyAttribute), true).Length > 0).Name);
                var sql = $"UPDATE {typeof(T).Name} SET {string.Join(", ", properties.Select(p => $"{p.Name} = @{p.Name}"))} WHERE {typeof(T).GetProperties().First(p => p.GetCustomAttributes(typeof(KeyAttribute), true).Length > 0).Name} = @{typeof(T).GetProperties().First(p => p.GetCustomAttributes(typeof(KeyAttribute), true).Length > 0).Name}";
                return await db.ExecuteAsync(sql, entity);
            }
        }

        // 根据 ID 删除实体
        public async Task<int> DeleteAsync(int id)
        {
            using (IDbConnection db = new SqlConnection(_connectionString))
            {
                var sql = $"DELETE FROM {typeof(T).Name} WHERE {typeof(T).GetProperties().First(p => p.GetCustomAttributes(typeof(KeyAttribute), true).Length > 0).Name} = @Id";
                return await db.ExecuteAsync(sql, new { Id = id });
            }
        }
    }
}
